#-*- coding:utf8 -*-
import config
from lib import verify 
from app.base import BaseHandler
import time
import hashlib

class MemberHandler(BaseHandler):
    def get(self):
        self.display('reg.html', msg=[], parame={})
            
    def post(self):
        email = self.get_argument('email', '')
        name = self.get_argument('fullname', '')
        headline = self.get_argument('headline', '')
        parame = {"email": email, "name": name, "headline": headline}
        isverify = True
        msg = []
        if not verify.IsEmail(email):
            msg.append(u"请输入正确的邮箱地址")
            isverify = False
        else:
            sql = "SELECT `email` FROM `user` WHERE `email` = '%s'" %(email)
            conn = config.getDBConnect()
            try:
                row = conn.get(sql)
                if len(row) <> 0:
                    msg.append(u"该邮箱地址已经存在")
                    parame['email'] = ''
                    isverify = False
            except:
                pass
            
        if not verify.IsName(name):
            msg.append(u"姓名应为 2-7 个汉字，或 4-14 个字母")
            isverify = False
            
        if len(headline) < 4 or len(headline) > 128:
            msg.append(u"个人介绍应为 4-128个字符")
            isverify = False
            
        if isverify:
            conn = config.getDBConnect()
            pwd = hashlib.new("md5", "123456").hexdigest()
            ip = self.request.remote_ip
            sql = "INSERT INTO `user` (`email`, `password`, `name`, `reg_ip`, `reg_time`, `signature`) VALUE('%s', '%s', '%s', %d, %d, '%s')" %(email, pwd, name, config.ip2bigint(ip), time.time(), headline)
            try:
                conn.execute(sql)
            except:
                msg.append(u"注册失败")
            
        if not isverify:
            self.display('reg.html', msg=msg, parame=parame)
        else:
            self.display('reg_success.html', msg=msg)